﻿Imports System.Collections.Generic
''' <summary>
''' VS.NET reportviewer helper class to identify and print group page numbers
''' NOTE:
''' This class currently supports for windows application. If you want to use it web application then introduce a logic
''' to uniquely identify the dictionaries because it is using shared / static varibales. This can be easily achieved by having
''' one more dictoinary i.e. Dictionary(Of String, Dictionary) whose key will be the logged on user name and the value will be the 
''' actual dictionary hold the data.
''' </summary>
''' <remarks></remarks>
Public Class GroupTotalPage
    ''' <summary>
    ''' Dictionary to hold the Group Name and the page number at which this group ends
    ''' </summary>
    ''' <remarks></remarks>
    Private Shared _GroupWithRunningMaxPageNumber As New Dictionary(Of String, String)
    ''' <summary>
    ''' Dictionary to hold the Group Name and the Total number of pages in that group
    ''' </summary>
    ''' <remarks></remarks>
    Private Shared _GroupWithTotalPageNumber As New Dictionary(Of String, String)

    ''' <summary>
    ''' Adds the group and the page number into the shared/static dictionary collection
    ''' The groupname / id being the key and pagenumber as value 
    ''' If the groupname already exists then the GroupCurrentPageNumber value will be reset with the 
    ''' surrent value. By doing so, always we will have the maximum value in each group
    ''' which will be the group's total number of page once the report is processed completely 
    ''' This method is being called from the code in the rdlc file
    ''' </summary>
    ''' <param name="group"></param>
    ''' <param name="groupCurrentPageNumber"></param>
    ''' <remarks></remarks>
    Public Sub Add(ByVal group As String, ByVal groupCurrentPageNumber As String)
        If _GroupWithRunningMaxPageNumber.ContainsKey(group) Then
            _GroupWithRunningMaxPageNumber(group) = groupCurrentPageNumber
        Else
            _GroupWithRunningMaxPageNumber.Add(group, groupCurrentPageNumber)
        End If
    End Sub
    ''' <summary>
    ''' This Gives the Group and the Total number of pages within the group
    ''' Internally this function reads the data stored in the dictionary <see>_GroupAndMaxPageNumberWithInTheGroup</see>
    ''' and computes the result.
    ''' <see>_GroupAndMaxPageNumberWithInTheGroup</see> will have the data like below which is the input for this function
    ''' [group-A,04] ------- (1)
    ''' [group-B,09] ------- (2)
    ''' [group-C,10] ------- (3)
    ''' This function iterates the <see>_GroupAndMaxPageNumberWithInTheGroup</see> collection and computes the
    ''' total number of pages by substracting the page number with the previous page number like below
    ''' At Iteration-01
    '''          Copys as it is from _GroupWithRunningMaxPageNumber into _GroupWithTotalPageNumber 
    '''          The result will be like [group-A,04]
    ''' At Iteration-02
    '''          It substracts (2) - (1) i.e. 09-04=05
    '''          The result will be like 
    '''                                [group-A,04]
    '''                                [group-B,05]
    ''' At Iteration-03
    '''          It substracts (3) - (2) i.e. 10-09=01
    '''          The result will be like 
    '''                                [group-A,04]
    '''                                [group-B,05]
    '''                                [group-C,01]
    ''' </summary>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Function GetGroupWithTotalPageNumber() As Dictionary(Of String, String)
        If _GroupWithTotalPageNumber.Count = 0 Then
            Dim preKey As String = String.Empty
            For Each dic In _GroupWithRunningMaxPageNumber
                If String.IsNullOrEmpty(preKey) Then
                    _GroupWithTotalPageNumber.Add(dic.Key, dic.Value)
                    preKey = dic.Key
                Else
                    _GroupWithTotalPageNumber.Add(dic.Key, (CInt(dic.Value) - CInt(_GroupWithRunningMaxPageNumber(preKey))).ToString())
                    preKey = dic.Key
                End If
            Next
        End If
        Return _GroupWithTotalPageNumber
    End Function
    ''' <summary>
    ''' This gives the Total number of pages for a given group(key)
    ''' This looks up at the dictionary <see>_GroupWithTotalPageNumber</see> and returns the value for the matching key
    ''' This is called from the code residing in the rdlc for each page to print the total number of pages
    ''' </summary>
    ''' <param name="key"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Function GetTotalPageNumber(ByVal key As String) As String
        If Not String.IsNullOrEmpty(key) AndAlso _GroupWithTotalPageNumber.ContainsKey(key) Then
            Return _GroupWithTotalPageNumber(key)
        Else
            Return String.Empty
        End If
    End Function
    ''' <summary>
    ''' Resets the shared variables.
    ''' </summary>
    ''' <remarks></remarks>
    Public Sub Clear()
        _GroupWithRunningMaxPageNumber.Clear()
        _GroupWithTotalPageNumber.Clear()
    End Sub

End Class

